﻿Public Class frm_upload

    Dim MyThread As Threading.Thread

    Delegate Sub delegChangeStatus(ByVal Status As String)
    Delegate Sub deledAddPeserta(ByVal NoUN As String, ByVal Nama As String)
    Delegate Sub delegEnableButton(ByVal Param As Boolean)

    Sub safeChangeStatus(ByVal Status As String)
        Dim lastRow As Integer = Me.ListView1.Items.Count - 1
        Try
            Me.ListView1.Items(lastRow).SubItems(2).Text = Status
        Catch ex As Exception

        End Try
    End Sub

    Sub safedAddPeserta(ByVal NoUN As String, ByVal Nama As String)
        Dim lastRow As Integer

        Me.ListView1.Items.Add(NoUN)

        lastRow = Me.ListView1.Items.Count - 1

        Me.ListView1.Items(lastRow).SubItems.Add(Nama)
        Me.ListView1.Items(lastRow).SubItems.Add("Uploading...")

    End Sub

    Sub safeEnableButton(ByVal Param As Boolean)
        Me.Button2.Enabled = Param
    End Sub

    Sub BeginUpload()

        'stop thread cek koneksi
        SignalUpload = True

        ''# Kamus Data
        Dim rcd_peserta As Odbc.OdbcDataReader
        Dim i As Integer = 0
        Dim STS As String
        Dim ChangeStatus As New delegChangeStatus(AddressOf safeChangeStatus)
        Dim AddPeserta As New deledAddPeserta(AddressOf safedAddPeserta)
        Dim EnableButton As New delegEnableButton(AddressOf safeEnableButton)

        '# get Peserta
        DB.FlushCache()
        DB.Selects("*")
        DB.From("tbl_pendaftaran")
        DB.Where("status", "0")

        rcd_peserta = Connection.ExecuteToDataReader(DB.GetQueryString)

        If rcd_peserta.HasRows Then
            With rcd_peserta

                While .Read
                    'Try

                    Me.Invoke(AddPeserta, .Item("no_uan").ToString, .Item("nama_pendaftar").ToString)

                    '# Begin Upload
                    '---------------------------------------------------------------------------------------
                    SOCKET.SendMessage("STUDENT RESET")

1:                  STS = SOCKET.SendMessage("STUDENT REGISTER_ID " & .Item("no_pendaftaran").ToString.Replace("-", vbNullString))
                    If STS.Substring(0, 1) <> "0" Then
                        Me.Invoke(ChangeStatus, STS)
                        Continue While
                    ElseIf STS = "Error" Then
                        Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                        SignalUpload = False : Exit Sub
                    End If

2:                  STS = SOCKET.SendMessage("STUDENT UN " & .Item("no_uan").ToString.Replace("-", vbNullString))
                    If STS.Substring(0, 1) <> "0" Then
                        Me.Invoke(ChangeStatus, STS)
                        Continue While
                    ElseIf STS = "Error" Then
                        Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                        SignalUpload = False : Exit Sub
                    End If

3:                  STS = SOCKET.SendMessage("STUDENT NAME " & .Item("nama_pendaftar").ToString)
                    If STS.Substring(0, 1) <> "0" Then
                        Me.Invoke(ChangeStatus, STS)
                        Continue While
                    ElseIf STS = "Error" Then
                        Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                        SignalUpload = False : Exit Sub
                    End If

4:                  STS = SOCKET.SendMessage("STUDENT OLD_SCHOOL " & .Item("asal_sekolah").ToString)
                    If STS.Substring(0, 1) <> "0" Then
                        Me.Invoke(ChangeStatus, STS)
                        Continue While
                    ElseIf STS = "Error" Then
                        Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                        SignalUpload = False : Exit Sub
                    End If

5:                  STS = SOCKET.SendMessage("STUDENT GENDER " & IIf(.Item("jenis_kelamin").ToString = "L", "M", "F"))
                    If STS.Substring(0, 1) <> "0" Then
                        Me.Invoke(ChangeStatus, STS)
                        Continue While
                    ElseIf STS = "Error" Then
                        Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                        SignalUpload = False : Exit Sub
                    End If

6:                  STS = SOCKET.SendMessage("STUDENT IS_BANDUNG " & IIf(.Item("asal_pendaftar").ToString = 0, "1", "0"))
                    If STS.Substring(0, 1) <> "0" Then
                        Me.Invoke(ChangeStatus, STS)
                        Continue While
                    ElseIf STS = "Error" Then
                        Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                        SignalUpload = False : Exit Sub
                    End If

7:                  STS = SOCKET.SendMessage("STUDENT BIRTHDATE " & .Item("tanggal_lahir").ToString)
                    If STS.Substring(0, 1) <> "0" Then
                        Me.Invoke(ChangeStatus, STS)
                        Continue While
                    ElseIf STS = "Error" Then
                        Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                        SignalUpload = False : Exit Sub
                    End If

8:                  STS = SOCKET.SendMessage("STUDENT GRADUATE_YEAR " & .Item("tahun_lulus").ToString)
                    If STS.Substring(0, 1) <> "0" Then
                        Me.Invoke(ChangeStatus, STS)
                        Continue While
                    ElseIf STS = "Error" Then
                        Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                        SignalUpload = False : Exit Sub
                    End If

9:                  STS = SOCKET.SendMessage("STUDENT FIRST_CHOICE " & .Item("pilihan_1").ToString)
                    If STS.Substring(0, 1) <> "0" Then
                        Me.Invoke(ChangeStatus, STS)
                        Continue While
                    ElseIf STS = "Error" Then
                        Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                        SignalUpload = False : Exit Sub
                    End If

10:                 STS = SOCKET.SendMessage("STUDENT SECOND_CHOICE " & .Item("pilihan_2").ToString)
                    If STS.Substring(0, 1) <> "0" Then
                        Me.Invoke(ChangeStatus, STS)
                        Continue While
                    ElseIf STS = "Error" Then
                        Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                        SignalUpload = False : Exit Sub
                    End If

                    ' total nilai
11:                 STS = SOCKET.SendMessage("STUDENT NTOTAL " & .Item("n_total").ToString.Replace(",", "."))
                    If STS.Substring(0, 1) <> "0" Then
                        Me.Invoke(ChangeStatus, STS)
                        Continue While
                    ElseIf STS = "Error" Then
                        Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                        SignalUpload = False : Exit Sub
                    End If

                    'nilai indo & matematika
12:                 STS = SOCKET.SendMessage("STUDENT N1 " & .Item("n_indonesia").ToString.Replace(",", "."))
                    If STS.Substring(0, 1) <> "0" Then
                        Me.Invoke(ChangeStatus, STS)
                        Continue While
                    ElseIf STS = "Error" Then
                        Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                        SignalUpload = False : Exit Sub
                    End If

13:                 STS = SOCKET.SendMessage("STUDENT N2 " & .Item("n_matematika").ToString.Replace(",", "."))
                    If STS.Substring(0, 1) <> "0" Then
                        Me.Invoke(ChangeStatus, STS)
                        Continue While
                    ElseIf STS = "Error" Then
                        Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                        SignalUpload = False : Exit Sub
                    End If

                    'nilai berdasarkan jenjang
                    If APP.Info.Jenjang = "SMP" Then
142:                    STS = SOCKET.SendMessage("STUDENT N3 " & .Item("n_ipa").ToString.Replace(",", "."))
                        If STS.Substring(0, 1) <> "0" Then
                            Me.Invoke(ChangeStatus, STS)
                            Continue While
                        ElseIf STS = "Error" Then
                            Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                            SignalUpload = False : Exit Sub
                        End If

                    ElseIf APP.Info.Jenjang = "SMA" Then
143:                    STS = SOCKET.SendMessage("STUDENT N3 " & .Item("n_ipa").ToString.Replace(",", "."))
                        If STS.Substring(0, 1) <> "0" Then
                            Me.Invoke(ChangeStatus, STS)
                            Continue While
                        ElseIf STS = "Error" Then
                            Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                            SignalUpload = False : Exit Sub
                        End If

153:                    STS = SOCKET.SendMessage("STUDENT N4 " & .Item("n_inggris").ToString.Replace(",", "."))
                        If STS.Substring(0, 1) <> "0" Then
                            Me.Invoke(ChangeStatus, STS)
                            Continue While
                        ElseIf STS = "Error" Then
                            Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                            SignalUpload = False : Exit Sub
                        End If

                    Else ' SMK
144:                    STS = SOCKET.SendMessage("STUDENT N3 " & .Item("n_ipa").ToString.Replace(",", "."))
                        If STS.Substring(0, 1) <> "0" Then
                            Me.Invoke(ChangeStatus, STS)
                            Continue While
                        ElseIf STS = "Error" Then
                            Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                            SignalUpload = False : Exit Sub
                        End If

154:                    STS = SOCKET.SendMessage("STUDENT N4 " & .Item("n_inggris").ToString.Replace(",", "."))
                        If STS.Substring(0, 1) <> "0" Then
                            Me.Invoke(ChangeStatus, STS)
                            Continue While
                        ElseIf STS = "Error" Then
                            Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                            SignalUpload = False : Exit Sub
                        End If

16:                     STS = SOCKET.SendMessage("STUDENT N5 " & .Item("n_agama").ToString.Replace(",", "."))
                        If STS.Substring(0, 1) <> "0" Then
                            Me.Invoke(ChangeStatus, STS)
                            Continue While
                        ElseIf STS = "Error" Then
                            Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                            SignalUpload = False : Exit Sub
                        End If

17:                     STS = SOCKET.SendMessage("STUDENT N6 " & .Item("n_ips").ToString.Replace(",", "."))
                        If STS.Substring(0, 1) <> "0" Then
                            Me.Invoke(ChangeStatus, STS)
                            Continue While
                        ElseIf STS = "Error" Then
                            Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                            SignalUpload = False : Exit Sub
                        End If

18:                     STS = SOCKET.SendMessage("STUDENT N7 " & .Item("n_pkn").ToString.Replace(",", "."))
                        If STS.Substring(0, 1) <> "0" Then
                            Me.Invoke(ChangeStatus, STS)
                            Continue While
                        ElseIf STS = "Error" Then
                            Me.Invoke(ChangeStatus, "404 - Tidak bisa terhubung dengan server. Cek koneksi internet!")
                            SignalUpload = False : Exit Sub
                        End If


                    End If

                    STS = SOCKET.SendMessage("STUDENT SAVE")

                    '# Begin Upload
                    '---------------------------------------------------------------------------------------

                    Me.Invoke(ChangeStatus, "Sukses")

                    ' sukses
                    ' update status peserta jadi 1
                    DB.FlushCache()

                    DB.Update("tbl_pendaftaran")
                    DB.SetField("status", "1")
                    DB.Where("no_pendaftaran", .Item("no_pendaftaran").ToString)

                    Connection.ExecuteNonQuery(DB.GetQueryString)

                    'Catch ex As Exception
                    '    MsgBox("Error in BeginUpload : " & ex.Message, MsgBoxStyle.Exclamation, "Pesan Error")
                    'End Try
                End While

            End With
        Else
            MsgBox("Tidak ada data untuk di-upload", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Pesan")
        End If

        'start thread cek koneksi
        SignalUpload = False

        Me.Invoke(EnableButton, True)
        MyThread.Abort()
    End Sub

    Private Sub frm_upload_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.ListView1.Items.Clear()
        Button1.Enabled = False
        Button2.Enabled = False
        MyThread = New Threading.Thread(AddressOf BeginUpload)
        MyThread.Start()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
End Class